import axios from "axios";
import nprogress from "nprogress";
import { getTempId } from "@/utils";
import { getToken } from "@/utils";
import "nprogress/nprogress.css";

const requests = axios.create({
  baseURL: "/api",
  timeout: 5000,
});
requests.interceptors.request.use((config) => {
  config.headers.userTempId = getTempId();
  getToken() && (config.headers.token = getToken());
  nprogress.start();
  return config;
});
requests.interceptors.response.use(
  ({ data }) => {
    nprogress.done();
    return data;
    /* else {
      alert(data.message);
      return Promise.reject(data.message);
    } */
  },
  () => {
    alert("服务器响应数据失败");
  }
);
export default requests;
